/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.report.paymentorder.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.apestar.common.config.Global;
import cn.apestar.common.persistence.Page;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.report.paymentorder.entity.PaymentOrder;
import cn.apestar.modules.report.paymentorder.service.PaymentOrderService;

/**
 * 充值记录报表Controller
 * @author yudw
 * @version 2016-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/paymentorder/paymentOrder")
public class PaymentOrderController extends BaseController {

    @Autowired
    private PaymentOrderService paymentOrderService;

    @ModelAttribute
    public PaymentOrder get(@RequestParam(required = false) String id) {
        PaymentOrder entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = paymentOrderService.get(id);
        }
        if (entity == null) {
            entity = new PaymentOrder();
        }
        return entity;
    }

    @RequestMapping(value = {"list" , ""})
    public String list(PaymentOrder paymentOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (paymentOrder.getStatus() == null)
            paymentOrder.setStatus("success");
        Page<PaymentOrder> page = paymentOrderService.findPage(new Page<PaymentOrder>(request, response), paymentOrder);
        model.addAttribute("page", page);
        return "modules/report/report/paymentorder/paymentOrderList";
    }

    @RequestMapping(value = "form")
    public String form(PaymentOrder paymentOrder, Model model) {
        model.addAttribute("paymentOrder", paymentOrder);
        return "modules/report/paymentorder/paymentOrderForm";
    }

    @RequestMapping(value = "save")
    public String save(PaymentOrder paymentOrder, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, paymentOrder)) {
            return form(paymentOrder, model);
        }
        paymentOrderService.save(paymentOrder);
        addMessage(redirectAttributes, "保存充值记录报表成功");
        return "redirect:" + Global.getAdminPath() + "/paymentorder/paymentOrder/?repage";
    }

    @RequestMapping(value = "delete")
    public String delete(PaymentOrder paymentOrder, RedirectAttributes redirectAttributes) {
        paymentOrderService.delete(paymentOrder);
        addMessage(redirectAttributes, "删除充值记录报表成功");
        return "redirect:" + Global.getAdminPath() + "/paymentorder/paymentOrder/?repage";
    }

}